<template>
	<div>
		<div class="container">
			<div class="handle-box">
			  <label class="label" >文档类型:</label>
			  <el-select class="handle-select"
			     v-model="resourceType" 
				filterable
				allow-create
			   >
				<el-option
					v-for="item in options.types"
					:key="item.value"
					:label="item.label"
					:value="item.value"
					/>
                    </el-select>
				<label class="label" >名称:</label>
				<el-input class="handle-input" v-model="name"></el-input>
				<label class="label" >时间:</label> 
				<el-date-picker
                 class="picker"
                    v-model="timeSearch"
                    type="daterange"
                    range-separator="到"
                    />
                <el-button color="#0ab394" @click="handleSearch">查询</el-button>
				<!-- <el-button color="#1c84c0" :icon="Search" @click="handleSearch">查询</el-button> -->
                <el-row class="top-btn">
                    <el-button color="#1c84c0"  @click="open">添加</el-button>
                    <el-button type="danger"  @click="handleAllDelete">删除</el-button>
                </el-row>
			</div>
			<el-table ref="multipleTableRef" 
			:header-cell-style="{color:'#2A62BC'}"
			 :data="tableData"  
			  style="width: 100%;" 
			  size="small"
              border 
              stripe 
			  @selection-change="handleSelectionChange">
                <el-table-column   type="selection" width="35" align="center" />
				<el-table-column prop="name" label="资源名称" align="center" ></el-table-column>
                <el-table-column prop="describe" label="资源描述" align="center" ></el-table-column>
                <el-table-column prop="type" label="类型" align="center" ></el-table-column>
                <el-table-column prop="size" label="大小" align="center" ></el-table-column>
                <el-table-column prop="classify" label="分类" align="center" ></el-table-column>
                <el-table-column prop="time" label="上传时间" align="center" ></el-table-column>
				<el-table-column label="操作" width="220" align="center" >
					<template #default="scope">
                        <el-row class="right-btn">
                            <el-tooltip content="修改" placement="top" effect="light" >
                                <el-button text :icon="Edit"  @click="open(scope.$index,scope.row)" v-permiss="2"></el-button>
                            </el-tooltip>
                            <el-tooltip content="删除" placement="top" effect="light" >
                                <el-button text :icon="Delete" class="red" @click="handleDelete(scope.$index)" v-permiss="2"></el-button>
                            </el-tooltip> 
					  
					</el-row>
					</template>
				</el-table-column>
			</el-table>
			<div class="pagination">
				<el-pagination
				    :current-page="query.pageIndex"
					:page-size="query.pageSize"
					:page-sizes="[5, 10, 15, 20]"
					:small="small"
					layout="total, sizes, prev, pager, next, jumper"
					:total="pageTotal"
					@size-change="handleSizeChange"
					@current-change="handlePageChange"/>
			</div>
		</div>

		<!-- 修改或添加弹出框 -->
		<el-dialog :title="titleEdit" v-model="editVisible" width="30%" >
			<el-form label-width="70px">
				<el-form-item label="资源名称">
					<el-input v-model="form.name"></el-input>
				</el-form-item>
				<el-form-item label="选择文件">
                    <el-input type="file" placeholder=""></el-input> 
				</el-form-item>
				<el-form-item label="资源类型">
					<el-select class="handle-select"
                    v-model="form.type" 
                    filterable
                    allow-create
	            >
				<el-option
					v-for="item in options.types"
					:key="item.value"
					:label="item.label"
					:value="item.value"
					/>
                    </el-select>
				</el-form-item>            
                <el-form-item label="资源描述">
					<el-input v-model="form.describe" type="textarea"></el-input>
				</el-form-item>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="editVisible = false">关闭</el-button>
					<el-button color="#0ab394" @click="saveEdit">保存</el-button>
				</span>
			</template>
		</el-dialog>

		
	</div>
</template>

<script setup lang="ts" name="adminList">
import { ref, reactive } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { Delete, Edit, Search, Plus } from '@element-plus/icons-vue';
import { getResources} from '../../api/resource';
//是否启用
const  isStart = ref(true)

//分页字体
const small = ref(true)
const size = ref<'default' | 'large' | 'small'>('default')

const  name  = ref('')
const resourceType = ref('')
const timeSearch = ref('')
const options = reactive({
    types:[
        {
            value: 'pdf',
            label: 'pdf',
 	     },
        {
            value: 'docx',
            label: 'docx',
 	     },
        {
            value: 'ppt',
            label: 'ppt',
 	     },
        {
            value: 'txt',
            label: 'txt',
 	     },
    ],
   

})
//分页数据
const query = reactive({
	pageIndex: 1,
	pageSize: 5,
});
//

//表格数据
const tableData = ref();
//总页数
const pageTotal = ref(0);
const multipleTableRef = ref()
const state = reactive({
	multipleSelection:[]
})

// 获取表格数据  分页是数据截取  后台结合一起后  直接返回分页的后的数据
const getData = () => {
	
    getResources({pageCurrent:query.pageIndex,pageSize:query.pageSize}).then(res=>{
        console.log(res,"---res---")
        // tableData.value = res.data.
		let start  = (query.pageIndex-1)*query.pageSize;//开始位置
        let end =query.pageIndex*query.pageSize  ;//结束位置 数组的截取的时候 [start,end) suo所以不用-1
        tableData.value = res.data.slice(start,end);
        // tableData.value = res.data;
        pageTotal.value = res.data.length
    })
	
};
getData();
//多选框权限
const handleSelectionChange = (val) => {
  state.multipleSelection = val
}
// 分页导航
const handleSizeChange = (val) => {
  // console.log(`${val} items per page`)
  query.pageSize = val;
  getData()
}
const handlePageChange = (val) => {
	query.pageIndex = val;
	console.log(val,"==")
	console.log(query.pageSize)
	getData()
};



// 删除单个操作
const handleDelete = (index) => {
	// 二次确认删除
	ElMessageBox.confirm('确定要删除吗？', '提示', {
		type: 'warning'
	})
	.then(() => {
		ElMessage.success('删除成功');
		//to.... 传递id 到后台 
		tableData.value.splice(index, 1);
		// 删除成功后
		// getData()
	})
	.catch(() => {});
};
//删除多个数据  to......
const handleAllDelete =()=>{
	// console.log('删除多个')
	console.log(state.multipleSelection,'===拿到所有选择的数据====')
	if (state.multipleSelection.length==0) {
		ElMessage.error('请选择项')
		return
  	}
	// to...
	//axios删除的操作
	

	
}

// 查询操作  to... 需要自己实现与后台交互
const handleSearch = () => {
	query.pageIndex = 1;
	getData();
};

// 表格编辑时弹窗和保存
const editVisible = ref(false);
let form = reactive({
    id:'',
    name:'',
    describe:'',
    type:''
});

// 
let titleEdit = ref('修改')
let isDisable = ref(true)
const open  = (index,row)=>{//index是时间源对象
	if(row){
		isDisable.value=true
		titleEdit.value = "修改"
		handleEdit(index,row)
	}else{
		//把第一个输出改为可编辑
		isDisable.value=false
		//清空表单中的数据
		titleEdit.value = "添加";
        form.name=''
        form.type = ''
        form.describe =''
		editVisible.value = true	
	}
	console.log('-------测试传值11-------')
	// console.log(index,row,index.target.innerText)
	// console.log(index,row)
	// console.log('================')

}
let idx = -1;
const handleEdit = (index, row) => {
	idx = index;
    form.name=row.name
    form.type = row.type
    form.describe =row.describe 
	editVisible.value = true;
};
//保存添加或者修改的数据 to---需要重新实现
const saveEdit = () => {
	editVisible.value = false;
	ElMessage.success(`修改第 ${idx + 1} 行成功`);
	tableData.value[idx].name = form.name;
	tableData.value[idx].describe = form.describe;
	tableData.value[idx].type = form.type;

};

import { genFileId } from 'element-plus'
// import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus'
const upload = ref()

const handleExceed = (files) => {
  upload.value!.clearFiles()
  const file = files[0]
  file.uid = genFileId()
  upload.value!.handleStart(file)
}

</script>

<style scoped>
.handle-box {
	margin-bottom: 20px;   
    .el-button{
        margin-left: 10px;
    }
}
.handle-select {
	width: 120px;
}
.handle-input {
	width: 120px;
}
.label{
   display: inline-block;
   max-width:100%;
   margin: 5px;
   font-weight: 700; 
}


.table {
	width: 100%;
	font-size: 13px;
}
.red {
	color: #F56C6C;
}
.blue {
	color: #1296e2;
	font-weight: bold;
}
.mr10 {
	margin-right: 10px;
}
.table-td-thumb {
	display: block;
	margin: auto;
	width: 40px;
	height: 40px;
}
.cell{
    display: inline-block;
    .el-button{
        margin-left: 0px;
    }
}

.top-btn{
    float: right;
}
.right-btn{
    justify-content: center;
	.el-button{
		background-color: #f1f1f1;
		width: 30px;
		height: 30px;
		margin-left: 10px;
	}
}

</style>
